#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
#define x first
#define y second
const int N = 2e5 + 10, M = N * 2;
int h[N], ne[M], w[M], e[M], idx;
int dist[N];
int n, m;
int a, b, c;
bool st[N];
void add(int a, int b ,int c)
{
	e[idx] = b;
	ne[idx] = h[a];
	w[idx] = c;
	h[a] = idx++;
}
void dijkstra()
{
	memset(dist, 0x3f, sizeof(dist));
	dist[1] = 0;
	priority_queue<PII, vector<PII>, greater<PII>>q;
	q.push({ 0,1 });
	while (q.size()) {
		PII t = q.top();
		q.pop();
		int ver = t.y;
		int distance = t.x;
		if (st[ver]) {
			continue;
		}
		st[ver] = 1;
		for (int i = h[ver]; ~i; i = ne[i]) {
			int j = e[i];	
			if (dist[j] > distance + w[i]) {
				dist[j] = distance + w[i];
				q.push({ dist[j],j});
			}
		}
	}
}

int main()
{
	scanf("%d%d", &n ,&m);
	memset(h, -1, sizeof(h));
	while (m--) {
		scanf("%d%d%d", &a, &b, &c);
		add(a, b, c);
		add(b, a, c);
	}
	dijkstra();
	if (dist[n] == 0x3f3f3f3f) {
		cout << "qwb baka" << endl;
	}
	else {
		cout << dist[n] << endl;
	}
	return 0;
}